<template>
  <div>
    <el-button type="primary" @click="getNetworkInterfaces">刷新</el-button>
    <el-table :data="netList" border  style="margin-top: 20px" v-loading="netLoading">
    <el-table-column prop="name" label="名称">
      <template #default="scope">
        <p>{{scope.row.name}}</p>
        <p>{{scope.row.desc}}</p>
      </template>
    </el-table-column>
    <el-table-column prop="ipv4" label="ipv4" />
    <el-table-column prop="metric" label="跃点数" />
    <el-table-column prop="ifIndex" label="ifIndex" />
    <el-table-column prop="status" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.status == 'Up' ? 'success' : 'danger'">
          {{ scope.row.status == 'Up' ? '开启' : '关闭' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="mac" label="mac" />
    <el-table-column prop="speed" label="速度" />
    <el-table-column label="操作" width="180">
      <template #default="scope">
        <el-button
          type="primary"
          size="small"
          @click="handleRestartAdapter(scope.row.name)"
          :loading="scope.row.restarting"
          style="margin-right: 8px"
        >
          重启
        </el-button>
        <el-button
          :type="scope.row.status === 'Up' ? 'danger' : 'success'"
          size="small"
          @click="handleToggleAdapter(scope.row)"
          :loading="scope.row.toggling"
        >
          {{ scope.row.status === 'Up' ? '禁用' : '启用' }}
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import type { NetIf } from 'main/types/src/type/interfaces.d.ts'
import { netList } from '@/global/store'

const netLoading = ref(false)

onMounted(() => {
  getNetworkInterfaces()
})



const getNetworkInterfaces = async () => {
  netLoading.value = true
  try {
    netList.value = await window.electron.getNetworkInterfaces()
  } finally {
    netLoading.value = false
  }
}


const handleRestartAdapter = async (name: string) => {
  const adapter = netList.value.find(item => item.name === name);
  if (!adapter) {
    return
  }
  try{
    adapter.restarting = true;
    const res = await window.electron.restartNetworkAdapter(name);
    console.log("handleRestartAdapter", res)
    // 等待2秒后刷新列表
    setTimeout(async () => {
      await getNetworkInterfaces();
    }, 2000);
  } catch (error) {
    console.error('重启网卡失败:', error);
    ElMessage.error('重启网卡失败 ' + error);
  }
  adapter.restarting = false;

}


const handleToggleAdapter = async (adapter: NetIf) => {
  const enable = adapter.status !== 'Up';
  try {
    adapter.toggling = true;
    const result = await window.electron.setNetworkAdapterStatus(adapter.name, enable);
    if (!result.success) {
      ElMessage.error(result.message);
      adapter.toggling = false;
      return;
    }
    // 等待2秒后刷新列表
    setTimeout(async () => {
      await getNetworkInterfaces();
    }, 2000);
  } catch (error) {
    console.error(`${enable ? '启用' : '禁用'}网卡失败:`, error);
    ElMessage.error(`${enable ? '启用' : '禁用'}网卡失败 ${error}`);
    adapter.toggling = false;
  }
}
</script>
